//
//  MYStatusCell.swift
//  MYWeibo
//
//  Created by Magic Yu on 16/4/16.
//  Copyright © 2016年 Magic Yu. All rights reserved.
//

import UIKit
import SnapKit

class MYStatusCell: UITableViewCell {
    
    //工具条的顶部约束对象
    var toolBarTopCons: Constraint?
    var statusViewModel: MYStatusViewModel? {
        didSet{
        originalView.statusViewModel = statusViewModel
//        statusToolBar.statusViewModel = statusViewModel
        retweetedView.statusViewModel = statusViewModel
            
            //解决没有转发微博时 转发微博视图仍在存在的问题
            //记录statusToolBar的顶部约束 判断当转发微博存在时 设置statusToolBar的顶部约束为转发微博  否则为原创微博
            //先设置转发微博占位 记录顶部约束 在判断之前卸载顶部约束 再根据微博类型创建新的约束
            
            //因为需要根据数据判断是否是转发微博 所以要在这这个方法中
            //1.卸载约束
            self.toolBarTopCons?.uninstall()
            if statusViewModel?.status?.retweeted_status != nil {
            
                //转发微博
                //设置约束
                statusToolBar.snp_makeConstraints(closure: { (make) -> Void in
                    toolBarTopCons = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                
                //显示转发微博
                retweetedView.hidden = false
                //设置数据源
                retweetedView.statusViewModel = statusViewModel
                
            }else {
            
                //原创微博
                //设置约束
                statusToolBar.snp_makeConstraints(closure: { (make) -> Void in
                    toolBarTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
                //隐藏转发微博
                retweetedView.hidden = true
                //设置数据源为nil
                retweetedView.statusViewModel = nil
            }
            
            
            
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
     
        //backgroundColor = randomColor()
        //添加子视图
        contentView.addSubview(originalView)
        //添加评论区
        contentView.addSubview(statusToolBar)
        //添加转发微博视图
        contentView.addSubview(retweetedView)
        
        //设置约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(contentView)
        }
        
        statusToolBar.snp_makeConstraints { (make) -> Void in
            //记录顶部约束
            self.toolBarTopCons = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(36)
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.right.equalTo(contentView)
            
        }
        
        //添加contentView的底部约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(statusToolBar.snp_bottom)
            
        }
    }
    
    //懒加载原创视图控件
    private lazy var originalView: MYStatusOriginalView = MYStatusOriginalView()
    //懒加载评论区
    private lazy var statusToolBar: MYStatusToolBar = MYStatusToolBar()
    //懒加载转发微博视图
    private lazy var retweetedView: MYStatusRetweetedView = MYStatusRetweetedView()
}
